﻿using RecruitStudents.Client.BLL;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecruitStudents.Client.Base
{
    public partial class Base_Teacher_ExamRoom_List : BaseForm
    {
        ServiceBase service = new ServiceBase();
        public Base_Teacher_ExamRoom_List()
        {
            InitializeComponent();
            this.BindRoom();
        }
        private void BindRoom()
        {
            listExamRoom.DataSource = null;
            using (DataTable dt = service.GetExamEntretenimientohRoomTable())
            {
                if (dt != null)
                {
                    listExamRoom.DisplayMember = "EntretenimientohExamRoomName";
                    listExamRoom.ValueMember = "ExamRoomNO";
                    listExamRoom.DataSource = dt;
                }
            }
        }
        private void BindTeacher()
        {
            listTeacher.DataSource = null;
            string strSpec = string.Empty;
            if (listExamRoom.SelectedItem != null)
            {
                DataRowView dv = listExamRoom.SelectedItem as DataRowView;
                strSpec = dv.Row["SpecialityKindsNO"].ToSafeString();
            }
            using (DataTable dt = service.GetExaminerTableByDropDown(SessionerHelper.SystemYear, listExamRoom.SelectedValue.ToSafeString(), strSpec))
            {
                if (dt != null)
                {
                    listTeacher.DisplayMember = "ExaminerName";
                    listTeacher.ValueMember = "ExaminerID";
                    listTeacher.DataSource = dt;
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                string strExamRoomNO = listExamRoom.SelectedValue.ToSafeString();
                string strExaminerID = listTeacher.SelectedValue.ToSafeString();
                List<TB_Exam_ExamRoomExaminerMapping> list = new List<TB_Exam_ExamRoomExaminerMapping>();
                DevExpress.XtraEditors.BaseListBoxControl.SelectedItemCollection selectItems = listTeacher.SelectedItems;
                for (int i = 0; i < selectItems.Count; i++)
                {
                    DataRowView dr = (DataRowView)selectItems[i];
                    TB_Exam_ExamRoomExaminerMapping model = new TB_Exam_ExamRoomExaminerMapping();
                    model.ExaminerID = dr["ExaminerID"].ToString();
                    model.ExamRoomNO = strExamRoomNO;
                    model.ExamYear = SessionerHelper.SystemYear;
                    model.IsActive = "1";
                    model.Remark = "";
                    list.Add(model);

                }
                service.SaveExaminerRoomMapping(list);
                BindTeacher();
                BindRoomTeacher();
            }
            catch (Exception ex)
            {
                DevHelper.ShowErrorMessageBox("操作数据出现错误:" + ex.Message);
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            try
            {
                DevExpress.XtraEditors.BaseListBoxControl.SelectedItemCollection selectItems = listTeacherExamRoom.SelectedItems;
                 
                for (int i = 0; i < selectItems.Count; i++)
                {
                    DataRowView dr = (DataRowView)selectItems[i];
                    if (dr != null)
                    {
                        string strID = dr["RoomTeacherID"].ToString();
                        string[] strDataSZ = strID.Split('|');
                        service.DeleteExaminerRoomMapping(strDataSZ[1], strDataSZ[0],  SessionerHelper.SystemYear);
                    }
                }
                BindTeacher();
                BindRoomTeacher();
            }
            catch (Exception ex)
            {
                DevHelper.ShowErrorMessageBox("操作数据出现错误:" + ex.Message);
            }
        }

        private void listExamRoom_SelectedIndexChanged(object sender, EventArgs e)
        {

            BindRoomTeacher();
            BindTeacher();
        }

        private void BindRoomTeacher()
        {
            string strExamRoomNO = listExamRoom.SelectedValue.ToSafeString();
            if (!string.IsNullOrEmpty(strExamRoomNO))
            {
                listTeacherExamRoom.DataSource = null;
                using (DataTable dt = service.GetExaminerRoomDataTable(strExamRoomNO, string.Empty, SessionerHelper.SystemYear))
                {
                    if (dt != null)
                    {
                        listTeacherExamRoom.DisplayMember = "RoomTeacher";
                        listTeacherExamRoom.ValueMember = "RoomTeacherID";
                        listTeacherExamRoom.DataSource = dt;
                    }
                }
            }
        }
    }
}
